using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Headspring.Training.Domain.Model;
using Headspring.Training.WebUI.Presenters;
using Headspring.Training.WebUI.Views;
using StructureMap;

namespace Headspring.Training.WebUI
{
	public partial class SearchWorkOrders : Page, ISearchWorkOrderView
	{
		private WorkOrderStatus[] _statuses;
		private Employee[] _assignees;
		private Employee[] _creators;
		private WorkOrder[] _workOrders;
		private SearchPresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			_presenter = ObjectFactory.FillDependencies<SearchPresenter>();
			_presenter.Initialize(this);

			if (!IsPostBack)
			{
				foreach (Employee employee in Creators)
				{
					ddlCreator.Items.Add(new ListItem(employee.GetFullName(), employee.UserName));
				}
				ddlCreator.Items.Insert(0, new ListItem("<Any>", ""));

				foreach (Employee employee in Assignees)
				{
					ddlAssignee.Items.Add(new ListItem(employee.GetFullName(), employee.UserName));
				}
				ddlAssignee.Items.Insert(0, new ListItem("<Any>", ""));

				foreach (WorkOrderStatus status in Statuses)
				{
					ddlStatus.Items.Add(new ListItem(status.ToString(), status.Key));
				}
				ddlStatus.Items.Insert(0, new ListItem("<Any>", ""));

				initializeDropDowns();


				_presenter.PerformSearch(this);
				grdWorkOrders.DataSource = _workOrders;
				grdWorkOrders.DataBind();
			}
		}

		private void initializeDropDowns()
		{
			string status = Request["status"];
			string creator = Request["creator"];
			string assignee = Request["assignee"];
			ddlCreator.SelectedValue = creator;
			ddlAssignee.SelectedValue = assignee;
			ddlStatus.SelectedValue = status;
		}

		public WorkOrderStatus GetSelectedStatus()
		{
			if(ddlStatus.SelectedIndex == 0)
			{
				return null;
			}

			return WorkOrderStatus.Parse(ddlStatus.SelectedValue);
		}

		public Employee GetSelectedAssignee()
		{
			Employee assignee = Array.Find(_assignees, delegate(Employee obj) { return obj.UserName == ddlAssignee.SelectedValue; });
			return assignee;
		}

		public Employee GetSelectedCreator()
		{
			Employee creator = Array.Find(_creators, delegate(Employee obj) { return obj.UserName == ddlCreator.SelectedValue; });
			return creator;
		}

		public void SetStatuses(WorkOrderStatus[] statuses)
		{
			_statuses = statuses;
		}

		public void SetAssignees(Employee[] assignees)
		{
			_assignees = assignees;
		}

		public void SetCreators(Employee[] creators)
		{
			_creators = creators;
		}

		public void SetMatchingWorkOrders(WorkOrder[] workOrders)
		{
			_workOrders = workOrders;
		}

		public WorkOrderStatus[] Statuses
		{
			get { return _statuses; }
		}

		public Employee[] Assignees
		{
			get { return _assignees; }
		}

		public Employee[] Creators
		{
			get { return _creators; }
		}

		protected void RedirectToSearch(object sender, EventArgs e)
		{
			_presenter.RequestSearch(this);
		}
	}
}